import sqlite3
from flask import Flask
from contextlib import closing

app = Flask(__name__)
from app import views

app.config['DATABASE'] = '/tmp/cogsci.db'

def connect_db():
    return sqlite3.connect(app.config['DATABASE'])

def init_db():
    with closing(connect_db()) as db:
        with app.open_resource('schema.sql') as f:
            db.cursor().executescript(f.read())
        db.commit()

society_dis = [
        ('CogScic103', 'CogSci c103/History c192/Media Studies c104C/Information c103', 'History of Information', 'Cognitive+Science', 'c103'),
        ('CogScic104', 'CogSci/Ling c104', 'The Mind, Language, and Politics', 'Cognitive+Science', 'c104'),
        ('Anthro166', 'Anthropology 166', 'Language, Culture, and Society', 'Anthropology', 166),
        ('Econ119', 'Economics 119', 'Psychology and Economics', 'Economics', 119),
        ('Educ140AC', 'Education 140AC', 'Literacy: Individual and Societal Development', 'Education', '140AC'),
        ('Ling150', 'Linguistics 150 ', 'Sociolinguistics', 'Linguistics', 150),
        ('Psych107', 'Psychology 107', 'Buddhist Psychology', 'Psychology', 107),
        ('Psych164', 'Psychology 166AC', 'Social Cognition', 'Psychology', 164),
        ('Psych166AC', 'Psychology 166AC', 'Cultural Psychology', 'Psychology', '166AC'),
        ('Sociol150', 'Sociology 150', 'Social Psychology', 'Sociology', 150),
        ('Sociol150A', 'Sociology 150A', 'Social Psychology: Self and Society', 'Sociology', '150A'),
        ('Psych160', 'Psychology 160', 'Social Psychology', 'Psychology', 160),

        ]

society_conc = [
    ('Anthro149', 'Anthropology 149', 'Psychological Anthropology', 'Anthropology', 149),
    ('Anthro160AC', 'Anthropology 160AC', 'Forms of Folklore', 'Anthropology', '160AC'),
    ('Anthro161', 'Anthropology 161', 'Narrative Folklore', 'Anthropology', 161),
    ('Info146', 'Information 146', 'Foundations of New Media', 'Information', 146),
    ('Ling130', 'Linguistics 130', 'Comparative and Historical Linguistics', 'Linguistics', 130),
    ('Lingc139', 'Ling/Slavic c139', 'Language Spread', 'Linguistics', 'c139'),
    ('Ling151', 'Linguistics 151', 'Language and Gender', 'Linguistics', 151),
    ('Ling170', 'Linguistics 170', 'History, Structure, and Sociolinguistics of a Particular Language', 'Linguistics', 170),
    ('Natamst151', 'Native American Studies 151','Native American Philosophy', 'Native+American+Studies', 151),
    ('Phil153', 'Philosophy 153', 'Chinese Philosophy', 'Philosophy', 153),
    ('PoliSci161', 'Political Science 161', 'Public Opinion, Voting, and Participation', 'Political+Science', 161),
    ('PoliSci164A', 'Political Science 164A', 'Political Psychology and Involement', 'Political+Science', '164A'),
    ('Psych167AC', 'Psychology 167AC', 'Stigma and Prejudice', 'Psychology', '167AC'),
    ('Rhetor103A', 'Rhetoric 103A', 'Approaches and Paradigms in the History of Rhetorical Theory', 'Rhetoric', '103A'),
    ('Rhetor105', 'Rhetoric 105', 'Rhetorical Theory and Practice in Historical Theory', 'Rhetoric', 105),
    ('Rhetor110', 'Rhetoric 110', 'Advanced Argumentative Writing', 'Rhetoric', 110),
    ('Rhetor170', 'Rhetoric 170', 'Rhetoric of Social Science', 'Rhetoric', 170),
    ('Rhetor174', 'Rhetoric 174', 'Rhetoric of Scientific Discourse', 'Rhetoric', 174),
    ('Rhetor175', 'Rhetoric 175', 'Rhetoric of Philosophical Discourse', 'Rhetoric', 175),
    ('Rhetor177', 'Rhetoric 177', 'Language, Truth, and Dialogue', 'Rhetoric', 177)
        ]

ling_dis = [
        ('Ling100', 'Linguistics 100', 'Introduction to Linguistic Science', 'Linguistics', 100),
        ('CogScic101', 'CogSci c101/Ling c105', 'The Mind and Language', 'Cognitive+Science', 'c101'),
        ('CogScic142', 'CogSci/Ling c142', 'Language and Thought', 'Cognitive+Science', 'c142')
        ]

ling_conc = [
    ('Ling106', 'Linguistics 106', 'Metaphor', 'Linguistics', 106),
    ('Ling110', 'Linguistics 110', 'Introduction to Phonetics and Phonology', 'Linguistics', 110),
    ('Cogscic108', 'CogSci Ling c108', 'The Challenge of Cognitive Science to Western Philosophy', 'Cognitive+Science', 'c108'),
    ('CogScic110', 'CogSci c110/Ling c109/CS c182', 'Neural Basis of Thought and Language', 'Cognitive+Science', 'c110'),
    ('Ling120', 'Linguistics 120', 'Introduction to Syntax and Semantics', 'Linguistics', 120),
    ('Ling115', 'Linguistics 115', 'Phonology and Morphology', 'Linguistics', 115),
    ('Ling121', 'Linguistics 121', 'Logical Semantics', 'Linguistics', 121),
    ('Ling123', 'Linguistics 123', 'Pragmatics', 'Linguistics', 123),
    ('Ling158', 'Linguistics 158', 'Computational Methods', 'Linguistics', 158),
    ('CogScic140', 'CogSci c140/Ling c160', 'Quantitative Methods in Linguistics', 'Cognitive+Science', 'c140'),
    ('Ling181', 'Linguistics 181', 'Lexical Semantics', 'Linguistics', 181),
    ('CogScic124', 'CogSci/Psych c124', 'Psycholinguistics', 'Cognitive+Science', 'c124'),
    ('Psych143', 'Psychology 143', 'Language Acquisition', 'Psychology', 143)
        ]

phil_dis = [
        ('Phil122', 'Philosophy 122', 'Theory of Knowledge', 'Philosophy', 122),
        ('Phil132', 'Philosophy 132', 'Philosophy of Mind', 'Philosophy', 132),
        ('Phil133', 'Philosophy 133', 'Philosophy of Language', 'Philosophy', 133),
        ('Phil135', 'Philosophy 135', 'Theory of Meaning', 'Philosophy', 135),
        ('Phil136', 'Philosophy 136', 'Philosophy of Perception', 'Philosophy', 136),
        ]

phil_conc = [
    ('Phil128', 'Philosophy 128', 'Philosophy of Science', 'Philosophy', 128),
    ('Phil130', 'Philosophy 130', 'Philosophy of Social Science', 'Philosophy', 130),
    ('Phil138', 'Philosophy 138', 'Philosophy of Society', 'Philosophy', 138),
    ('Phil140A', 'Philosophy 140A,B', 'Intermediate Logic', 'Philosophy', '140A'),
    ('Phil174', 'Philosophy 174', 'Locke', 'Philosophy', 174),
    ('Phil176', 'Philosophy 176', 'Hume', 'Philosophy', 176),
    ('Phil178', 'Philosophy 178', 'Kant', 'Philosophy', 178),
    ('Phil185', 'Philosophy 185', 'Heidegger', 'Philosophy', 185),
    ('Phil186', 'Philosophy 186', 'Wittgenstein', 'Philosophy', 186),
    ('Phil188', 'Philosophy 188', 'Phenomenology', 'Philosophy', 188),
    ('CogScic108', 'CogSci/Ling c108', 'The Challenge of Cognitive Science to WEstern Philosophy', 'Cognitive+Science', 'c108'),

        ]

neuro_dis = [
        ('Psych117', 'Psychology 117', 'Human Neuropsychology', 'Psychology', 117),
        ('CogScic127', 'CogSci/Psych c127', 'Cognitive Neuroscience', 'Cognitive+Science', 'c127'),
        ('Psych133', 'Psychology 133', 'Psychology of Sleep', 'Psychology', 133),
        ]

neuro_conc = [
    ('MCBc160', 'MCB/Neuroscience c160', 'Introduction to Neurobiology', 'Molecular+and+Cell+Biology', 'c160'),
    ('MCB163', 'Molecular and Cell Biology 163', 'Mammalian Neuroanatomy', 'Molecular+and+Cell+Biology', 163),
    ('MCB160L', 'Molecular and Cell Biology 160L', 'Neurobiology Lab', 'Molecular+and+Cell+Biology', '160L'),
    ('MCB164', 'Molecular and Cell Biology 164', 'Sensory and Integrative Neurobiology', 'Molecular+and+Cell+Biology', 164),
    ('MCB165', 'Molecular and Cell Biology 165', 'Molecular Neurobiology', 'Molecular+and+Cell+Biology', 165),
    ('MCB166' 'Molecular and Cell Biology 166', 'Biophysical Neurobiology', 'Molecular+and+Cell+Biology', 166),
    ('Psych110', 'Psychology 110', 'Biological Psychology', 'Psychology', 110),
    ('Psych111', 'Psychology 111', 'Sensory Processes: Vision', 'Psychology', 111),
    ('Psych114', 'Psychology 114', 'Biology of Learning and Neuroplasticity', 'Psychology', 114),
    ('CogScic110', 'CogSci c110/CS c182/Ling c109', 'Neural Basis of Thought and Language', 'Cognitive+Science', 'c110'),
    ('IB245', 'Integrative Biology  245/245L', 'Functional Neuroanatomy and Lab', 'Integrative+Biology', 245),

        ]

comp_dis = [
        ('CS188', 'Computer Science 188', 'Introduction to Artifical Intelligence', 'Computer+Science', 188),
        ('CogScic131', 'CogSci c131/Psych c123', 'Computational Models of Cognition', 'Cognitive+Science', 'c131'),
        ]

comp_conc = [
    ('CogScic110', 'CogSci c110/CS c182/Ling c109', 'Neural Basis of Thought and Language', 'Cognitive+Science', 'c110'),
    ('VisSci265', 'Vision Science 265', 'Neural Computation', 'Visual+Science', 265),
    ('CS160', 'Computer Science 160', 'User Interface Design and Development', 'Computer+Science', 160),
    ('CS170', 'Computer Science 170', 'Efficient Algorithms and Intractable Problems', 'Computer+Science', 170),
    ('CS186', 'Computer Science 186', 'Introduction to Database Systems', 'Computer+Science', 186),
    ('CSc280', 'CS/Vision Science c280', 'Computer Vision', 'Computer+Science', 'c280'),
    ('CS287', 'Computer Science 287', 'Advanced Robotics', 'Computer+Science', 287),
    ('CS288', 'Computer Science 288', 'Artifical Intelligence Approach to Natural Language Processing', 'Computer+Science', 288)
        ]

psych_dis = [
        ('CogScic100', 'CogSci c100/Psych c120', 'Basic Issues in Cognition', 'Cognitive+Science', 'c100'),
        ('Psych122', 'Psychology 122', 'Human Learning and Memory', 'Psychology', 122),
        ('CogScic124', 'CogSci/Psych c124', 'Psycholinguistics', 'Cognitive+Science', 'c124'),
        ('CogScic126', 'CogSci/Psych c126', 'Perception', 'Cognitive+Science', 'c126'),
        ('CogScic102', 'CogSci c102/Psych c129', 'Scientific Approaches to Consciousness', 'Cognitive+Science', 'c102'),
        ('Psych143', 'Psychology 143', 'Language Acquisition', 'Psychology', 143),
        ('Psych164', 'Psychology 164', 'Social Cognition', 'Psychology', 164),
        ]

psych_conc = [
    ('CogScic127', 'CogSci/Psych c127', 'Cognitive Neuroscience', 'Cognitive+Science', 'c127'),
    ('Psych107', 'Psychology 107', 'Buddhist Psychology', 'Psychology', 107),
    ('Psych111', 'Psychology 111', 'Sensory Processes: Vision', 'Psychology', 111),
    ('Psych121', 'Psychology 121', 'Animal Cognition', 'Psychology', 121),
    ('Psych133', 'Psychology 133', 'Psychology of Sleep', 'Psychology', 133),
    ('Music108', 'Music 108 or 108M', 'Music Perception and Cognition', 'Music', 'c108'),
    ('Educ224A', 'Education 224A', 'Mathematical Thinking and Problem Solving', 'Education', '224A'),
    ('Educ229A', 'Education 229A', 'Problem Solving and Understanding', 'Education', '229A')

        ]

pillars = ['society', 'ling', 'phil', 'neuro', 'comp', 'psych']
all_klasses = {}
for p in pillars:
    all_klasses[p] = { 1: eval(p+'_dis'), 2: eval(p+'_conc') }

# 0 is lower div req
# 1 is distribution requirement
# 2 is concentration requirement

def populate_db():
    db = connect_db()
    for p in all_klasses:
        pillar = all_klasses[p]
        for t in pillar:
            klasses = pillar[t]
            for k in klasses:
                if len(k) == 3:
                    db.execute('insert into klasses (abbr, title, description, pillar_type, req_type) values (?, ?, ?, ?, ?)', list(k)+[p, t])
                elif len(k) == 4:
                    db.execute('insert into klasses (abbr, title, description, pillar_type, req_type, offered) values (?, ?, ?, ?, ?, ?)', list(k[:3])+[p, t]+[k[3]])
                elif len(k) == 5:
                    db.execute('insert into klasses (abbr, title, description, pillar_type, req_type, department, course_num) values (?, ?, ?, ?, ?, ?, ?)', list(k[:3])+[p, t]+list(k[3:]))


    db.commit()
    db.close()

init_db()
populate_db()
